package com.xx.pmis.web.actions;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.xx.pmis.model.Card;
import com.xx.pmis.services.CardService;
import com.xx.pmis.web.forms.CardForm;
import com.xx.pmis.web.util.CurrentDate;

/**
 * @class 卡片Action类 <br>
 * 
 * @editor 石星星 @Date 2011-9-21 下午03:17:42 <br>
 * @version jdk_1.5.0
 */
public class CardAction extends PMISBaseAction {
	private static final Log log = LogFactory.getLog(CardAction.class);

	CardService cardService = getPMISServices().getCardService();

	// 查询所有卡片
	public ActionForward listCard(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		List<Card> all = null;
		try {
			all = cardService.getAllCard();
		} catch (Exception e) {
			log.info("查询卡片异常！" + e.toString());
		}
		request.setAttribute("cardList", all);
		request.setAttribute("position", "物资管理/卡片");
		return mapping.findForward("listCard");
	}

	// 新增
	public ActionForward addCard(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("editDate", CurrentDate.currentDate());
		request.setAttribute("position", "卡片/新增");
		return mapping.findForward("addCard");
	}

	// 新增，处理方法
	public ActionForward addCard_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		CardForm f = (CardForm) form;
		String bankName = f.getBankName(); // 项目名称
	    String bankCity = f.getBankCity(); // 归属城市
		String bankAddress = f.getBankAddress(); // 详细地址
		String accountCode = f.getAccountCode(); // 账号
		String cardState = f.getCardState(); // 状态，是否有网银
		String cardDesc = f.getCardDesc(); // 描述
		
		// 补充
		String balance = f.getBalance(); // 月结
		String editDate = f.getEditDate(); // 更新日期

		Card card = new Card();
		card.setBankName(bankName);
		card.setBankCity(bankCity);
		card.setBankAddress(bankAddress);
		card.setAccountCode(accountCode);
		card.setCardState(cardState);
		card.setCardDesc(cardDesc);
		card.setBalance(balance);
		card.setEditDate(editDate);
		
		try {
			cardService.saveOrUpdateCard(card);
		} catch (Exception e) {
			log.info("保存卡片异常！" + e.toString());
		}

		listCard(mapping, form, request, response);
		return mapping.findForward("listCard");
	}

	// 编辑
	public ActionForward editCard(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String cardID = request.getParameter("cardID");

		Card card = new Card();
		try {
			card = cardService.getCardByCardID(cardID);
		} catch (Exception e) {
			log.info("跟进卡片ID查询该卡片异常！" + e.toString());
		}

		request.setAttribute("card", card);
		request.setAttribute("editDate", CurrentDate.currentDate());
		request.setAttribute("position", "卡片/编辑");
		return mapping.findForward("editCard");
	}

	// 编辑，处理方法
	public ActionForward editCard_Do(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		CardForm f = (CardForm) form;
		String cardID = f.getCardID();
		String bankName = f.getBankName(); // 项目名称
	    String bankCity = f.getBankCity(); // 归属城市
		String bankAddress = f.getBankAddress(); // 详细地址
		String accountCode = f.getAccountCode(); // 账号
		String cardState = f.getCardState(); // 状态，是否有网银
		String cardDesc = f.getCardDesc(); // 描述
		
		// 补充
		String balance = f.getBalance(); // 月结
		String editDate = f.getEditDate(); // 更新日期

		Card card = null;
		try {
			card = cardService.getCardByCardID(cardID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		card.setBankName(bankName);
		card.setBankCity(bankCity);
		card.setBankAddress(bankAddress);
		card.setAccountCode(accountCode);
		card.setCardState(cardState);
		card.setCardDesc(cardDesc);
		card.setBalance(balance);
		card.setEditDate(editDate);
		try {
			cardService.saveOrUpdateCard(card);
		} catch (Exception e) {
			log.info("保存卡片异常！" + e.toString());
		}

		listCard(mapping, form, request, response);
		return mapping.findForward("listCard");
	}

	// 删除
	public ActionForward delCard(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		Card card = null;
		CardForm f = (CardForm) form;
		String[] ids = f.getIds();
		for (int i = 0; i < ids.length; i++) {
			try {
				card = cardService.getCardByCardID(ids[i]);
			} catch (Exception e) {
				log.info("跟进卡片ID查询该卡片异常！" + e.toString());
			}
			try {
				cardService.deleteCard(card);
			} catch (Exception e) {
				log.info("保存卡片异常！" + e.toString());
			}
		}

		listCard(mapping, form, request, response);
		return mapping.findForward("listCard");
	}
	
	// 明细
	public ActionForward detailCard(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) {
		String cardID = request.getParameter("cardID");

		Card card = new Card();
		try {
			card = cardService.getCardByCardID(cardID);
		} catch (Exception e) {
			log.info("跟进卡片ID查询该卡片异常！" + e.toString());
		}

		request.setAttribute("card", card);
		request.setAttribute("position", "卡片/明细");
		return mapping.findForward("detailCard");
	}
}